<template>
	<view class="page">
		<flowbefore :beflows="beflows" :status="status" :isBtnClick="isBtnClicks" @save="save" @submit="submit" :statusType="statusType">
			<template>
				<view class="padding-bottom-100">
					<form class="flowform">
						<view class="formlist inputtext" v-if="$formOperates.read('flowTitle',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle">
									<label>流程标题</label>
									<text class="required">*</text>
								</view>
								<view class="controlvalue">
									<input type="text" v-model="debit.flowTitle" name="flowTitle" :disabled="judge('flowTitle')" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" v-if="$formOperates.read('billNo',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>单号编号</label></view>
								<view class="controlvalue">
									<input type="text" v-model="debit.billNo" name="billNo" :disabled="judge('billNo')" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" @tap="flowUrgent" v-if="$formOperates.read('flowUrgent',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle">
									<label>紧急程度</label>
									<text class="required">*</text>
								</view>
								<view class="controlvalue">
									<input type="text" v-model="debit.flowUrgent" name="flowUrgent" placeholder="选择"/>
								</view>
								<view class="advanceicon iconfont icon-xiangyou"></view>
							</view>
						</view>
						<view class="formlist inputtext" v-if="$formOperates.read('departmental',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>所属部门</label></view>
								<view class="controlvalue">
									<input type="text" v-model="debit.departmental" name="departmental" :disabled="judge('departmental')" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" v-if="$formOperates.read('applyDate',formOperatesData)">
							<view class="formgroup">
								<view class="labletitle"><label>申请日期</label></view>
								<view class="controlvalue">
									<input type="text" v-model="debit.applyDate" name="applyDate" :disabled="judge('applyDate')" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext margin-top-20" v-if="$formOperates.read('staffName',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle">
									<label>员工姓名</label>
									<text class="required">*</text>
								</view>
								<view class="controlvalue">
									<input type="text" v-model="debit.staffName" name="staffName" placeholder="输入姓名" :disabled="judge('staffName')" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" v-if="$formOperates.read('staffPost',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle">
									<label>员工职务</label>
									<text class="required">*</text>
								</view>
								<view class="controlvalue">
									<input type="text" v-model="debit.staffPost" name="staffPost" placeholder="输入职务" :disabled="judge('staffPost')" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" v-if="$formOperates.read('staffId',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle">
									<label>员工编码</label>
									<text class="required">*</text>
								</view>
								<view class="controlvalue">
									<input type="text" v-model="debit.staffId" name="staffId" placeholder="输入编码" :disabled="judge('staffId')" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" v-if="$formOperates.read('loanMode',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle">
									<label>借款方式</label>
									<text class="required">*</text>
								</view>
								<view class="controlvalue">
									<input type="text" v-model="debit.loanMode" name="loanMode" placeholder="输入借款方式" :disabled="judge('loanMode')" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" v-if="$formOperates.read('transferAccount',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>转账账户</label></view>
								<view class="controlvalue">
									<input type="text" v-model="debit.transferAccount" name="transferAccount" placeholder="输入转账账户" :disabled="judge('transferAccount')" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" @tap="paymentMethod" v-if="$formOperates.read('paymentMethod',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle">
									<label>支付方式</label>
									<text class="required">*</text>
								</view>
								<view class="controlvalue">
									<input type="text" v-model="debit.paymentMethod" name="paymentMethod" placeholder="选择"/>
								</view>
								<view class="advanceicon iconfont icon-xiangyou"></view>
							</view>
						</view>
						<view class="formlist inputtext" v-if="$formOperates.read('amountDebit',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle">
									<label>借支金额</label>
									<text class="required">*</text>
								</view>
								<view class="controlvalue">
									<input type="digit" v-model="debit.amountDebit" name="amountDebit" placeholder="输入借支金额" :disabled="judge('amountDebit')" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" v-if="$formOperates.read('repaymentBill',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>还款票据</label></view>
								<view class="controlvalue">
									<input type="text" v-model="debit.repaymentBill" name="repaymentBill" placeholder="输入还款票据" :disabled="judge('repaymentBill')" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" @tap="teachingDate" v-if="$formOperates.read('teachingDate',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle">
									<label>还款日期</label>
									<text class="required">*</text>
								</view>
								<view class="controlvalue">
									<input type="text" v-model="debit.teachingDate" name="teachingDate" placeholder="选择"/>
								</view>
								<view class="advanceicon iconfont icon-xiangyou"></view>
							</view>
						</view>
						<view class="formlist" v-if="$formOperates.read('reason',formOperatesData)">
							<view class="formgroup  padding-17">
								<view class="labletitletar"><label>借款原因</label></view>
								<view class="controlvalue  uni-textarea">
									<textarea v-model="debit.reason" name="reason" maxlength="500" placeholder="输入" :disabled="judge('reason')"/>
								</view>
							</view>
						</view>
					</form>
					<view class="height-100"></view>
				</view>
			</template>
		</flowbefore>
		<w-picker :mode="mode" :pickerList="pickerList" ref="picker" :isSearchShow="isSearchShow"></w-picker>
	</view>
</template>

<script>
	import jnpf from '@/common/js/jnpf.js'
	import helper from '@/common/js/helper.js'
	import graceChecker from '@/common/js/graceChecker.js'
	import wPicker from '@/components/w-picker/w-picker.vue'
	import flowbefore from '@/components/work/flow-before.vue'
	
	let formRule=[
		{name:"flowTitle",checkType:"notnull",checkRule:"",errorMsg:"输入标题"},
		{name:"flowUrgent", checkType : "notnull", checkRule:"",  errorMsg:"选择紧急程度"},
		{name:"paymentMethod", checkType : "notnull", checkRule:"",  errorMsg:"选择支付方式"},
		{name:"teachingDate", checkType : "notnull", checkRule:"",  errorMsg:"选择还款日期"},
		{name:"loanMode", checkType : "notnull", checkRule:"",  errorMsg:"输入借款方式"},
		{name:"staffId", checkType : "notnull", checkRule:"",  errorMsg:"输入员工编码"},
		{name:"amountDebit", checkType : "money", checkRule:"",  errorMsg:"选择合格金额"},
	];
	export default {
		data() {
			return {
				mode:'',//底部弹窗分类
				isSearchShow:false,
				disabled:false,//启用
				status:'',//流程状态
				beflows:{
					operstate:"",
					processId:''
				},
				debit: {
					billNo: '',
					flowUrgent: '',
					flowTitle: jnpf.userInfo().realName+'的借支单',
					departmental:jnpf.userInfo().departmentName,
					applyDate:helper.toDate(new Date, 'yyyy-MM-dd'),
					staffName:'',
					staffPost:'',
					loanMode:'',
					staffId:'',
					transferAccount:'',
					teachingDate:'',
					paymentMethod:'',
					amountDebit:'',
					repaymentBill:'',
					reason:''
				},
				isBtnClicks:false,
				pickerList:[],
				urgentList:[{
					label: "普通",
					value:"1"
				},
				{
					label: "重要",
					value:"2"
				},
				{
					label: "紧急",
					value:"3"
				}],
				paymentMethodList:jnpf.dictionaryData('WFSettlementMethod'),
				statusType:'',
				formOperatesData:[],
				F_Status: 0
			}
		},
		// created() {
		// 	this.$eventHub.$off('formOperates');
		// 	this.$eventHub.$on('formOperates', (data) => {
		// 		console.log(data)
		// 		this.formOperatesData = data
		// 		uni.setStorageSync('formOperatesData',data)
		// 	})
		// },
		onLoad(e) {
			/* 工作区进入状态 */
			let statusType = uni.getStorageSync("type")
			this.statusType = statusType
			let F_Status = uni.getStorageSync('F_Status')
			if(F_Status == 1 || F_Status == 2){
				this.disabled = true;
			}
			/* 工作区进入状态 End*/
			let flowInfo= JSON.parse(e.flowInfo);
			this.beflows.operstate=flowInfo.operstate;
			if(flowInfo.operstate =='add'){
				this.debit.flowId=flowInfo.id;
				this.status = 0;
				uni.setStorageSync('F_Status', 0);
				this.F_Status = Number(uni.getStorageSync('F_Status'))
				this.debit.flowUrgent = helper.getSpinnerNew(this.urgentList,'1');
				helper.ajax({
					url: '/api/system/Base/BillRule/BillNumber/WF_DebitBillNo',
					success: res => {
						this.debit.billNo=res.data.data;
					}
				});
				this.getOperatesData(flowInfo.id)
			}
			if(flowInfo.operstate=='info'){
				uni.setStorageSync('F_Status', F_Status);
				this.F_Status = Number(uni.getStorageSync('F_Status'))
				this.disabled=flowInfo.disabled;
				this.beflows.processId=flowInfo.id;
				this.status=flowInfo.status;
				helper.ajax({
					url: '/api/workflow/Form/DebitBill/' + flowInfo.id,
					success: res => {
						let _debit=res.data.data;
						this.debit = _debit;
						this.debit.flowUrgent = helper.getSpinnerNew(this.urgentList,_debit.flowUrgent);
						this.debit.paymentMethod = helper.getSpinnerNew(this.paymentMethodList,_debit.paymentMethod);
						this.debit.applyDate=helper.toDate(_debit.applyDate, 'yyyy-MM-dd');
						this.debit.teachingDate=helper.toDate(_debit.teachingDate, 'yyyy-MM-dd HH:mm');
					}
				});
				if(this.status == 0 || this.status == 3){
					this.getOperatesData(flowInfo.flowId)
				}else{
					this.$eventHub.$off('formOperates');
					this.$eventHub.$on('formOperates', (data) => {
						this.formOperatesData = data;
						console.log(this.formOperatesData)
						uni.setStorageSync('formOperatesData', data)
					})
				}
			}
		},
		methods: {
			getOperatesData(id){
				helper.ajax({
					url: '/api/workflow/Engine/FlowEngine/'+id,
					success: res => {
						let kk = JSON.parse(res.data.data.flowTemplateJson);
						this.formOperatesData = [];
						this.formOperatesData = kk.properties.formOperates
						uni.setStorageSync('formOperatesData',this.formOperatesData)
					}
				});
			},
			//判断表单权限可写
			judge(id) {
				return this.statusType == 'backlog' ? this.$formOperates.write(id, this.formOperatesData) : this.F_Status ==
					1 || this.F_Status == 2 ? true : this.$formOperates.write(id, this.formOperatesData)
			},
			flowUrgent(e) {
				uni.hideKeyboard();//隐藏软键盘  
				let isWrite = this.judge('flowUrgent')
				if(!isWrite){
					this.pickerList = this.urgentList;
					this.mode="selector";
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.debit.flowUrgent = data.result;
						this.$eventHub.$off('popupmodality');
					});
				}
			},
			teachingDate(e){
				uni.hideKeyboard();//隐藏软键盘  
				let isWrite = this.judge('teachingDate')
				if(!isWrite){
					this.mode="dateTime";
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.debit.teachingDate = data.result;
						this.$eventHub.$off('popupmodality');
					});
				}
			},
			paymentMethod(e){
				uni.hideKeyboard();//隐藏软键盘  
				let isWrite = this.judge('paymentMethod')
				if(!isWrite){
					this.pickerList= this.paymentMethodList;
					this.mode="selector";
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.debit.paymentMethod = data.result;
						this.$eventHub.$off('popupmodality');
					});
				}
			},
			
			submit(e){
				let formData=this.debit;
				//根据表单权限过滤必填列表
				formRule = graceChecker.filterRequiredList(formRule,this.formOperatesData)
				if (!graceChecker.check(formData,formRule)) {
					helper.msg(graceChecker.error);
					return;
				}
				this.isBtnClicks=true;
				formData.flowUrgent = helper.setSpinnerNew(this.urgentList,formData.flowUrgent);
				formData.paymentMethod = helper.setSpinnerNew(this.paymentMethodList,formData.paymentMethod);
				formData.status = 0;
				let method = "POST";
				let url = "/api/workflow/Form/DebitBill";
				if(formData.id){
					method = "PUT";
					url=url+"/"+formData.id
				}
				formData.applyDate = Date.parse(formData.applyDate);
				formData.teachingDate = Date.parse(formData.teachingDate);
				helper.ajax({
					url: url,
					method:method,
					contentType: "application/json",
					data:formData,
					success: res => {
						helper.msg(res.data.msg, 'success');
						if (this.beflows.operstate=='add') {
							uni.redirectTo({
								url: '../../operate/started/started',
								success: res => {},
								fail: () => {},
								complete: () => {}
							});
						}else{
							uni.navigateBack();
						}
						this.isBtnClicks=false
					}
				});
			},
			save(e){
				let formData=this.debit;
				//根据表单权限过滤必填列表
				formRule = graceChecker.filterRequiredList(formRule,this.formOperatesData)
				if (!graceChecker.check(formData,formRule)) {
					helper.msg(graceChecker.error);
					return;
				}
				this.isBtnClicks=true;
				formData.flowUrgent = helper.setSpinnerNew(this.urgentList,formData.flowUrgent);
				formData.paymentMethod = helper.setSpinnerNew(this.paymentMethodList,formData.paymentMethod);
				formData.status = 1;
				let method = "POST";
				let url = "/api/workflow/Form/DebitBill";
				if(formData.id){
					method = "PUT";
					url=url+"/"+formData.id
				}
				formData.applyDate = Date.parse(formData.applyDate);
				formData.teachingDate = Date.parse(formData.teachingDate);
				helper.ajax({
					url: url,
					method:method,
					contentType: "application/json",
					data:formData,
					success: res => {
						helper.msg(res.data.msg, 'success');
						if (this.beflows.operstate=='add') {
							uni.redirectTo({
								url: '../../operate/started/started',
								success: res => {},
								fail: () => {},
								complete: () => {}
							});
						}else{
							uni.navigateBack();
						}
						this.isBtnClicks=false
					}
				});
			},
		},
		components: {
			wPicker,
			flowbefore,
		}
	}
</script>

<style>
	.required{
		color: red;
	}
</style>
